import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import blexp from '../views/Experiment/BlExp.vue'
import ipexp from '../views/Experiment/IpExp.vue'
import grade from '../views/Grade/Grade.vue'
import exexp from '../views/Experiment/ExExp.vue'
import Login from '../views/Login/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component:Login,
      meta: { breadcrumb: '登录' },
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      children:[
        {
          path: '',
          name: 'ipexp',
          component:ipexp,
          meta: { breadcrumb: '数字孪生实验室-实验控制-内压实验' }
        },
        {
          path: 'ipexp',
          name: 'ipexp',
          component:ipexp,
          meta: { breadcrumb: '数字孪生实验室-实验控制-内压实验' }
        },
        {
          path: 'exexp',
          name: 'exexp',
          component:exexp,
          meta: { breadcrumb: '数字孪生实验室-实验控制-外压实验' }
        },
        {
          path: 'blexp',
          name: 'blexp',
          component:blexp,
          meta: { breadcrumb: '数字孪生实验室-实验控制-爆破实验' }
        },
        {
          path: 'grade',
          name: 'grade',
          component:grade,
          meta: { breadcrumb: '数字孪生实验-管理界面' }
        }
      ]
    }
  ]
})
// router.beforeEach((to, from, next) => {
//   //判断是否是登录保护路由
//   if (to.matched.some(record => record.meta.requiresAuth)) {
//       //验证浏览器本地存储是否有token（或从vuex、Cookie、localStorage中获取，依据自己的存储），用于判断是否登录
//       if (!sessionStorage.token) {//没有token，表示未登录，则重定向到login
//           next({
//               //这里是你登录页面的路由名称，或直接使用 path:'/login'
//               name: 'Login',
//               query: { redirect: to.fullPath }
//           })
//       } else {
//           next()
//       }
//   } else {
//       // 确保一定要调用 next()
//       next()
//   }
// })
router.beforeEach((to,from,next)=>{
  if(to.path==='/login') return next();
  //获取名字为名字为token的token令牌
  const token = window.sessionStorage.getItem('token')
  //不存在则直接跳转到登录界面
  if(!token) return next('/login')
  //否则直接跳转回主页
  next()
})
export default router
